app.controller("billingBillCtrl", function ($scope,$interval, $http, billingBillService) {

    //----------分页参数------------
    $scope.pageRowSize = 10;
    $scope.curPage = 1;

    //-----------------------------

    $scope.object = {};
    $scope.formMode = false;
    $scope.errors = {};

    $scope.isSuperAdmin = false;

    //--------------初始化菜单---------------
    $scope.initMenu = function () {
        showSecondMenu('billingManage');
        selectSecondMenu('billingBillQuery');
    }
    $scope.initMenu();

    //---------------错误信息--------------
    $scope.requestError = function () {
        $scope.errorMessage = "unknown error!"
    };

    $scope.init = function () {
        var span_superAdmin = $("#span_superAdmin").text();
        if(span_superAdmin == "true"){
            $scope.isSuperAdmin = true;
        }

        $scope.billingCycle = getMonthFunction(-1);  //日期
        $scope.loadBillingBill();
    };

    //查询计费帐单
    $scope.loadBillingBill = function () {
        $scope.pageRowSize = 10;
        $scope.curPage = 1;
        loadPage(findBillingBill, $scope, $scope.pageRowSize);
    };
    function findBillingBill(curPage,pageRow,flag) {
        $.fn.jqLoading({ height: 100, width: 240, text: "查询中，请稍等...." });
        var params = {action:"findBillingBill",pageNum:curPage,pageSize:pageRow};
        if(!NullOrUndefined($scope.billingCycle)){
            params["billingCycle"] = $scope.billingCycle;
        }
        if(!NullOrUndefined($("#contractUserSel").val())){
            params["contractUserId"] = $("#contractUserSel").val();
        }
        billingBillService.findBillingBill(params, function (data) {
            $.fn.jqLoading("destroy");
            if (data.status == 'SUCCESS'){
                $scope.pageList = data.data.voList;
                $scope.size = $scope.pageList.pages;
                $scope.billingBills = $scope.pageList.list;

                if ($scope.size > 0) {
                    $('.page-container').show();
                    var page = {
                        curPage: $scope.pageList.pageNum,
                        totalPage: $scope.size,
                        rowCount: $scope.pageList.total
                    };
                    if(flag){
                        $('.page-container').setPage(page);
                    }else{
                        $('.page-container').refreshPage(page);
                    }
                } else {
                    $('.page-container').hide();
                }
            } else {
                promptMsg(data.errorReason);
            }
        });
    }

    //导出文件
    $scope.exportFile = function (billingFileName,filePath) {
        window.location.href = "/billingbill/exportfile?billingFileName=" + billingFileName + "&filePath=" + filePath;
    }

    //打开上传执行账单窗口
    $scope.toUploadExecuteFile = function (id) {
        $('#id').val(id);
        $('#executeFile').val("");
        //模态框居中： 将事件监听的事件改成show.bs.modal 即可解决
        $('#editExecuteFileModal').on('show.bs.modal', centerModals("editExecuteFileModal"));
        $("#editExecuteFileModal").modal({
            backdrop: "static",//点击空白处不关闭对话框
            keyboard:false // 按esc键盘不关闭
        });
    }
    //上传执行账单
    $scope.submitExecuteFileForm = function () {
        //文件非空判断
        if ($('#executeFile').val() == null || $('#executeFile').val() == "") {
            promptMsg("请上传执行账单文件！");
            return false;
        }
        if (checkFileNameLength($('#executeFile'))) {
            promptMsg("文件名过长！文件名长度不能超过60！");
            return false;
        }
        uploadFile('updateExecuteFileForm','/billingbill/editExecuteFile',findBillingBills,"#editExecuteFileModal");
    }
    function findBillingBills() {
        findBillingBill($scope.curPage,$scope.pageRowSize,false);
    }

    //关闭上传执行账单窗口
    $scope.closeUploadExecuteFile = function(){
        $("#editExecuteFileModal").modal("hide");
    };

    //编辑计费账单
    $scope.editBillingBill = function () {
        //获取表中的数据，放入数组中传入后台
        var billingBillsArr = new Array();
        var flag = true;
        $("#tbody_billingBills").find("tr[name='insertBillingBills_tr']").each(function(){
            var idValue = $(this).children("td").children("input[name='idValue']").val();
            var executeAmountValue = $(this).children("td").children("input[name='executeAmountValue']").val();
            var remarkValue = $(this).children("td").children("input[name='remarkValue']").val();
            //最后插入的计费阶梯
            billingBillObject = {};
            billingBillObject.id = idValue;
            billingBillObject.executeAmount = executeAmountValue;
            billingBillObject.remark = remarkValue;
            billingBillsArr.push(billingBillObject);
        });
        //调取后台，进行编辑入库
        billingBillService.editBillingBill({
            action: 'editBillingBill',
            billingBillEntityVOs : billingBillsArr
        }, function (data) {
            if (data.status == "SUCCESS") {
                promptMsg(data.data.msg);
            } else {
                promptMsg(data.errorReason);
            }
        }, $scope.requestError());
    }

    //导出excel
    $scope.exportExcel = function () {
        if(NullOrUndefined($scope.billingCycle)){
            $scope.billingCycle = "";
        }
        window.location.href = "/billingbill/excel?billingCycle=" + $scope.billingCycle + "&contractUserId=" + $("#contractUserSel").val();
    }
});